#include "BuffEffectCfg.h"
#include "Common/TableData/SkillBuffCfg.h"

#include <iostream>

BuffBagEffectCfgMgr::BuffBagEffectCfgMgr()
{

}

BuffBagEffectCfgMgr::~BuffBagEffectCfgMgr()
{

}

bool BuffBagEffectCfgMgr::Init()
{
	const SkillBuffCfgMap *pMap = g_GetSkillBuffCfgTable()->GetSkillBuffCfgMap();
	if (!pMap)
	{
		return false;
	}

	SkillBuffCfgMap::const_iterator it = pMap->begin();
	for (; it != pMap->end(); it++)
	{
		uint32_t buffBagId = it->first;
		BuffBagEffectCfgInfo& buffBagCfgInfo = m_buffBagMap[buffBagId];
		buffBagCfgInfo.m_pCfgInfo = &(it->second);
		CommonApi::SplitStrToVecInt(buffBagCfgInfo.m_pCfgInfo->endType, ",", &buffBagCfgInfo.m_endType);

		if (buffBagCfgInfo.m_pCfgInfo->startType == (int32_t)BuffBagStatus::BUFF_BAG_STATUS_SKILL_TRIGGER_START
				|| buffBagCfgInfo.m_pCfgInfo->startType == (int32_t)BuffBagStatus::BUFF_BAG_STATUS_PROP_AND_SKILL_TRIGGER_START)
		{
			CommonApi::SplitStrToVecInt(buffBagCfgInfo.m_pCfgInfo->startTypeValue, ",", &buffBagCfgInfo.m_startTypeValue);
			if (buffBagCfgInfo.m_pCfgInfo->startType == (int32_t)BuffBagStatus::BUFF_BAG_STATUS_PROP_AND_SKILL_TRIGGER_START)
			{
				if (buffBagCfgInfo.m_startTypeValue.size() >= 2)
				{
					buffBagCfgInfo.m_startTypeValueProf = buffBagCfgInfo.m_startTypeValue[0];
					buffBagCfgInfo.m_startTypeValueSkillType = buffBagCfgInfo.m_startTypeValue[1];
				}
			}
		}


		for (size_t i = 0; i < it->second.vecSkillBuffEffectCfg.size(); i++)
		{
			
			const SkillBuffEffectCfgInfo &cfgInfo = it->second.vecSkillBuffEffectCfg[i];
			BuffEffectCfgInfo info;
			info.m_type = cfgInfo.Type;
			CommonApi::SplitStrToVecInt(cfgInfo.Param, ",", &info.m_param);

			if (info.m_type == effectTypeNone)
			{
				break;
			}
			else if (info.m_type == effectTypeDisperseMark)
			{
				buffBagCfgInfo.m_disperseEffectMark = CommonApi::StrToInt(cfgInfo.Param);
			}
			else if (info.m_type == effectTypeImmuneMark)
			{
				buffBagCfgInfo.m_immuneEffectMark = CommonApi::StrToInt(cfgInfo.Param);
			}
			else if (info.m_type == effectTypeDisperseBuffBagId)
			{
				buffBagCfgInfo.m_disperseBuffBagId.push_back(CommonApi::StrToInt(cfgInfo.Param));
			}
			else if (info.m_type == effectTypeImmuneBuffBagId)
			{
				buffBagCfgInfo.m_immuneBuffBagId.push_back(CommonApi::StrToInt(cfgInfo.Param));
			}

			buffBagCfgInfo.m_buffList.push_back(info);
		}
	}
	return true;
}

bool BuffBagEffectCfgMgr::UnInit()
{
	m_buffBagMap.clear();
	return true;
}

const BuffBagEffectCfgInfo* BuffBagEffectCfgMgr::FindBuffBag(uint32_t buffBagId) const
{
	BuffBagMapConstIter it = m_buffBagMap.find(buffBagId);
	if (it != m_buffBagMap.end())
	{
		return &(it->second);
	}
	return nullptr;
}


